package com.dh.leetcode.one;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: _118_Pascals_triangle
 * @Description: 118. 杨辉三角
 * https://leetcode-cn.com/problems/pascals-triangle/
 * 给定一个非负整数 numRows，生成杨辉三角的前 numRows 行。
 * @Author: shouzimu
 * @Date: 2020/11/14 15:24
 */
public class _118_Pascals_triangle {
    public List<List<Integer>> generate(int numRows) {
        //value = pre[i-1]+pre[i]
        numRows = numRows < 0 ? 1 : numRows;
        List<List<Integer>> list = new ArrayList<>(numRows);
        for (int i = 1; i <= numRows; i++) {
            List<Integer> l = new ArrayList<>(i);
            if (i == 1) {
                l.add(i);
            } else {
                List<Integer> pre = list.get(i - 2);
                for (int j = 1; j <= i; j++) {
                    int index = j - 1;
                    int preL = (index - 1 >= 0) ? pre.get(index - 1) : 0;
                    int preR = (index < i - 1) ? pre.get(index) : 0;
                    l.add(preL + preR);
                }
            }
            list.add(l);
        }
        return list;
    }


    @Test
    public void generateTest() {
        //leetcode 0ms
        List<List<Integer>> list = generate(34);
        int i = 0;
        for (List<Integer> integerList : list) {
            System.out.println("arry[" + (i++) + "] = new int[]{" + integerList.stream().map(t -> String.valueOf(t)).collect(Collectors.joining(
                    ",")) + "};");
        }
    }
}
